<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ShopsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        //
        $data = DB::table('shops_details')->join('shops','shops.id','=','shops_details.id')->where('shops.status','=',1)->get();
        return view('webadmin.shop.index',['data'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate()
    {
        // 
        $data = DB::table('shops_details')->join('shops','shops.id','=','shops_details.id')->where('shops.status','=',0)->get();
        return view('webadmin.shop.create',['data'=>$data]);
    }

    /**
     * 请求通过审批
     */
    public function getUpdate(Request $request,$id)
    {
        $status = DB::table('shops')->where('id','=',$id)->value('status');
         //自动切换状态
        function status($a){
          if($a=='0'){return 1;}else{return 0;};
        }
        $arr['status'] = status($status);
        $res = DB::table('shops')->where('id','=',$id)->update($arr);
        if($res){
            return redirect('/admin/shop/index');
        }else{
            return back();
        }
       
    }
    

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postDelete($id)
    {
        DB::beginTransaction();
        $res  = DB::table('shops')->where('id','=',$id)->delete();
        $resa = DB::table('shops_details')->where('id','=',$id)->delete();
        if($res && $resa) {
            DB::commit();
            return redirect('/admin/shop/create');
        }else{
            DB::rollBack();
            return back();
        }
    }
}
